home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 127 / PC Guia 127.iso / Software / Produtividade / OpenOffice.org 2.0.1 / openofficeorg4.cab / test_signal.py < prev    next >
Text File  |  2005-11-19  |  1KB  |  66 lines

  1. # Test the signal module
  2. from test.test_support import verbose, TestSkipped, TestFailed
  3. import signal
  4. import os, sys, time
  5.  
  6. if sys.platform[:3] in ('win', 'os2') or sys.platform=='riscos':
  7.     raise TestSkipped, "Can't test signal on %s" % sys.platform
  8.  
  9. if verbose:
  10.     x = '-x'
  11. else:
  12.     x = '+x'
  13. pid = os.getpid()
  14.  
  15. # Shell script that will send us asynchronous signals
  16. script = """
  17.  (
  18.         set %(x)s
  19.         sleep 2
  20.         kill -5 %(pid)d
  21.         sleep 2
  22.         kill -2 %(pid)d
  23.         sleep 2
  24.         kill -3 %(pid)d
  25.  ) &
  26. """ % vars()
  27.  
  28. def handlerA(*args):
  29.     if verbose:
  30.         print "handlerA", args
  31.  
  32. HandlerBCalled = "HandlerBCalled"       # Exception
  33.  
  34. def handlerB(*args):
  35.     if verbose:
  36.         print "handlerB", args
  37.     raise HandlerBCalled, args
  38.  
  39. signal.alarm(20)                        # Entire test lasts at most 20 sec.
  40. signal.signal(5, handlerA)
  41. signal.signal(2, handlerB)
  42. signal.signal(3, signal.SIG_IGN)
  43. signal.signal(signal.SIGALRM, signal.default_int_handler)
  44.  
  45. os.system(script)
  46.  
  47. print "starting pause() loop..."
  48.  
  49. try:
  50.     while 1:
  51.         if verbose:
  52.             print "call pause()..."
  53.         try:
  54.             signal.pause()
  55.             if verbose:
  56.                 print "pause() returned"
  57.         except HandlerBCalled:
  58.             if verbose:
  59.                 print "HandlerBCalled exception caught"
  60.             else:
  61.                 pass
  62.  
  63. except KeyboardInterrupt:
  64.     if verbose:
  65.         print "KeyboardInterrupt (assume the alarm() went off)"
  66.